package cn.diancan.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 

public class DateUtil {
	
	
/*	public static String currentDateTime(){
		Calendar cal=Calendar.getInstance();
		SimpleDateFormat dateformat2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
	    String a2=dateformat2.format(cal.getTime());
	    return a2;
	}*/

	/**
	 * 当前时间
	 * yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String currentDateTime(){
		return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
	}
	 
	
	/**
	 * 当前时间
	 * yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String currentTime(){
		return (new SimpleDateFormat("HH:mm:ss")).format(new Date());
	}
	
	public static String currentTime(Date date){
		return (new SimpleDateFormat("HH:mm:ss")).format(date);
	}
	
	
	
/*	public static long strDate2Long(String strDate){
		 try {
			SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dt2 = sdf.parse(strDate);
			 //继续转换得到秒数的long型
			long lTime = dt2.getTime() / 1000;
			return lTime;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}*/
	
	public static long strDate2Longfors(String strDate) throws ParseException{
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dt2 = sdf.parse(strDate);
			 //继续转换得到秒数的long型
		long lTime = dt2.getTime() / 1000;
		return lTime;
	}
	

	public static long strDate2Longforms(String strDate) throws ParseException{
		 
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dt2 = sdf.parse(strDate);
			 //继续转换得到秒数的long型
		return dt2.getTime();
		 
	}
	
	/**
	 *
	 */
	
	/**
	 * 当前时间
	 * yyyyMMddHHmmss
	 * @return
	 */
	public static String currentDateTime2(){
		return (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date());
	}
	public static String currentDateTime3(){
		return (new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(new Date());
	}
	
	/**
	 * 当前时间
	 * yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String currentDateTime(Date date){
		return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
	}
	/**
	 * 当前日期
	 * yyyy-MM-dd
	 * @return
	 */
	public static String currentDate(){
		return (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
	}
	
	
	 
	/**
	 * 判断是否 日期是当天
	 * @param d1
	 * @return
	 */
	public static Boolean isToday(String d1 ){
	   String today=(new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
	   if(null ==d1 || !today.equals(d1)){
			return false;
	   }
	   return true;
	}
	
	
	/**
	 * 当前日期
	 * yyyy-MM-dd
	 * @return
	 */
	public static String currentDate(Date date){
		return (new SimpleDateFormat("yyyy-MM-dd")).format(date);
	}
	
	/**
	 * 当前日期
	 * yyyyMMdd
	 * @return
	 */
	public static String currentDate2(Date date){
		return (new SimpleDateFormat("yyyyMMdd")).format(date);
	}
	
	
	public static String currentToString(){
		return (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date());
	}
	
	public static boolean isDateStringValid(String date){
	     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	     try {
			sdf.parse(date);
			return true;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	     
	  }

	public static Date DateTimeString2DateTime(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0); 
	    Date strtodate = sdf.parse(date, pos); 
		return strtodate;
	}
	/**
	 * date转String 
	 * @param date
	 * @return
	 */
	public static String DateTime2DateTimeString(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    String strtodate = sdf.format(date);
		return strtodate;
	}
	
	public static Date DateString2Date(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0); 
	    Date strtodate = sdf.parse(date, pos); 
		return strtodate;
	}
	
	/**
	 * 对比时间
	 * @param DATE1
	 * @param DATE2
	 * @return
	 */
	public static int compareDate(String DATE1, String DATE2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		 try {
			 Date dt1 = df.parse(DATE1);
			 Date dt2 = df.parse(DATE2);
			 if (dt1.getTime() > dt2.getTime()) {
				 return 1;
			 } else if (dt1.getTime() < dt2.getTime()) {
				 return -1;
			 } else {
				 return 0;
			 }
		 } catch (Exception exception) {
			 exception.printStackTrace();
		 }
		 return 0;
	}
	
	/**
	 * @param date //取时间 
	 * @param day 天数
	 * @return
	 */
	public static Date addDay(Date date,int day){
	     Calendar   calendar   =  new  GregorianCalendar(); 
	     calendar.setTime(date); 
	     calendar.add(Calendar.DATE,day);//把日期往后增加一天.整数往后推,负数往前移动 
	     date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
	     return date;
	}
	
	public static Date addHour(Date date,int hour){
	     Calendar   calendar   =  new  GregorianCalendar(); 
	     calendar.setTime(date); 
	     calendar.add(Calendar.HOUR_OF_DAY,hour);//把日期往后增加一天.整数往后推,负数往前移动 
	     date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
	     return date;
	}
	
	 
	
	public static String getMysqlDateTime(String dateTime){
	    if(null!= dateTime && dateTime.length()>=19){
		return dateTime.substring(0,19);
	    }
	    return dateTime;
	}
	
	
    /**
     * 判断当前日期是星期几 
     * @param dateTime 修要判断的时间
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */
    public static int dayForWeek(String dateTime)   {
	try {
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    Calendar c = Calendar.getInstance();
	    c.setTime(format.parse(dateTime));
	    int dayForWeek = 0;
	    if (c.get(Calendar.DAY_OF_WEEK) == 1) {
	        dayForWeek = 7;
	    } else {
	        dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
	    }
	    return dayForWeek;
	} catch (ParseException e) { 
	    e.printStackTrace();
	}
	return 0;
    }
    
    public static int dayForMonth(String dateTime)   {
	try {
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    Calendar c = Calendar.getInstance();
	    c.setTime(format.parse(dateTime));
	    
	    return c.get(Calendar.DAY_OF_MONTH);
	} catch (ParseException e) { 
	    e.printStackTrace();
	}
	return 0;
    }
	 
    public static void main(String[] args) {
	try {
	    System.out.println(dayForWeek("2016-02-29"));
	    System.out.println(dayForMonth("2016-02-01"));
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
	
}
